"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 功能测试
Case Name   : CTE+匿名块
Create At   : 2023/8/15
Owner       : n@ningyali
Description :
    1、建表插入数据
    2、CTE+匿名块
    3、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
History     :
"""

import os
import re
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CteCase0036(unittest.TestCase):
    def setUp(self):
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.log = Logger()
        self.t_name = 't_cte_0036'
        self.cte_name = 'cte_0036'
        text = f'-----{os.path.basename(__file__)} start-----'
        self.log.info(text)

    def test_1(self):
        text = '-----step1:建表插入数据; expect:成功-----'
        self.log.info(text)
        sql = f"drop table if exists {self.t_name};" \
            f"create table {self.t_name}(id int, name text);" \
            f"insert into {self.t_name} values (generate_series (1,1000), " \
            f"'abc'||generate_series (1,1000)%20);"
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res, f'执行失败：{text}')
        self.assertIn('INSERT 0 1000', res, f'执行失败：{text}')

        text = '-----step2:CTE+匿名块; expect:成功-----'
        self.log.info(text)
        sql = f'''
            declare
                type id_list is varray(6) of {self.t_name}.id%type;
                id_arr id_list;
            begin
                with {self.cte_name} as(
                    select id from {self.t_name} order by id desc limit 20
                )
                select id bulk collect into id_arr from {self.cte_name};
                raise notice 'id_arr = %', id_arr;
            end;'''
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        expect = r'NOTICE.*id_arr.*\{1000,999,998,997,996,995,994,993,' \
                 r'992,991,990,989,988,987,986,985,984,983,982,981\}.*'
        regex_res = re.search(expect, res, re.S)
        self.assertIsNotNone(regex_res, f'执行失败：{text}')
        self.assertIn('ANONYMOUS BLOCK EXECUTE', res, f'执行失败：{text}')

    def tearDown(self):
        text = '-----step3:清理环境; expect:成功-----'
        self.log.info(text)
        sql = f"drop table if exists {self.t_name};"
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res, f'执行失败：{text}')
        text = f'-----{os.path.basename(__file__)} end-----'
        self.log.info(text)
